<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
	* list users 
	* grant privileage user
	* edit, delete user
**/

class Cart extends CI_Controller {

	var $debug = '';
	var $per_page = '';

	public function __construct()
	{
		parent::__construct();	

		// gán giá trị tiêu đề form
		$this->template->set('nav',$this->uri->segment(1));
		$this->template->set('title', 'Giỏ hàng');
		$this->template->set('section', 'Test Giỏ hàng');

		// load model product
		$this->load->model('Product_model');
		$this->load->model('Order_model');
		$this->load->model('Order_sizes_model');		

		// load model kho
		$this->load->model('Storage_model');	
		// load model history kho
		$this->load->model('Storage_product_history_model');
		// load products that in storage
		$this->load->model('Storage_product_model');
		// load product size
		$this->load->model('Product_sizes_model');		
		// load model code_export_form
		$this->load->model('Code_export_form_model');
		// load model export temporary
		$this->load->model('Code_export_form_tmp_model');
		// 
		$this->load->model('Export_form_model');			
	}

	public function index()
	{
		// load products data
		$data['products'] = $this->Product_model->listProductsCart();	

		$success = $this->session->userdata('success');

		if(!empty($success))
		{
			$data['success'] = $success;
			$this->session->unset_userdata('success');
		}

		$tmp_products = array();
		foreach ($data['products'] as $product) 
		{
			$product_id = $product->id;
			$product_title = $product->title;
			$product_code = $product->code;

			$xs = $this->Storage_product_model->countQtyOfProductSize($product_code.'1');
			$s = $this->Storage_product_model->countQtyOfProductSize($product_code.'2');
			$m = $this->Storage_product_model->countQtyOfProductSize($product_code.'3');
			$l = $this->Storage_product_model->countQtyOfProductSize($product_code.'4');
			$xl = $this->Storage_product_model->countQtyOfProductSize($product_code.'5');
			$xxl = $this->Storage_product_model->countQtyOfProductSize($product_code.'6');
			$xxxl = $this->Storage_product_model->countQtyOfProductSize($product_code.'7');

			$tmp_products[] = array(
					'id'	=>	$product_id,
					'title'	=>	$product_title,
					'code'	=>	$product_code,
					'image'	=>	$product->image,
					'xs'	=>	$xs,
					's'		=>	$s,
					'm'		=>	$m,
					'l'		=>	$l,
					'xl'	=>	$xl,
					'xxl'	=>	$xxl,
					'xxxl'	=>	$xxxl,
				);
		}

		$data['products'] = $tmp_products;		

		$this->template->load('admin/template','admin/shopping_cart',$data);
	}

	public function destroy()
	{
		$this->cart->destroy();
		redirect('cart/index');
	}

	public function create($id)
	{
		$addToCartBtn = $this->input->post('addToCartBtn');

		$product = $this->Product_model->getEntry($id);

		// get id,price,name,color
		$product_id = $product->id;
		$product_code = $product->code;
		$price = $product->price;
		$product_name = $product->title;
		$product_color = $product->color;			

		// get quantity based on size
		$xs = $this->input->post('xs');
		$s = $this->input->post('s');
		$m = $this->input->post('m');
		$l = $this->input->post('l');
		$xl = $this->input->post('xl');
		$xxl = $this->input->post('xxl');
		$xxxl = $this->input->post('xxxl');		
		
		/////////////////////////////////////////////////////

		$items = array(
					array(	// xs
						'id'		=>	$product_id,
						'qty'		=>	$xs,
						'price'		=>	$price,
						'name'		=>	$product_name,
						'options'	=>	array('Size'=>'xs'),	
					),	// s
					array(	
						'id'		=>	$product_id,
						'qty'		=>	$s,
						'price'		=>	$price,
						'name'		=>	$product_name,
						'options'	=>	array('Size'=>'s'),	
					),
					array(	// m
						'id'		=>	$product_id,
						'qty'		=>	$m,
						'price'		=>	$price,
						'name'		=>	$product_name,
						'options'	=>	array('Size'=>'m'),	
					),
					array(	//l
						'id'		=>	$product_id,
						'qty'		=>	$l,
						'price'		=>	$price,
						'name'		=>	$product_name,
						'options'	=>	array('Size'=>'l'),	
					),
					array(	// xl
						'id'		=>	$product_id,
						'qty'		=>	$xl,
						'price'		=>	$price,
						'name'		=>	$product_name,
						'options'	=>	array('Size'=>'xl'),	
					),
					array(	// xxl
						'id'		=>	$product_id,
						'qty'		=>	$xxl,
						'price'		=>	$price,
						'name'		=>	$product_name,
						'options'	=>	array('Size'=>'xxl'),	
					),
					array(	// xxxl
						'id'		=>	$product_id,
						'qty'		=>	$xxxl,
						'price'		=>	$price,
						'name'		=>	$product_name,
						'options'	=>	array('Size'=>'xxxl'),	
					),
				);		
		
		//////////////// create order /////////////////////
		$customer_name = $this->input->post('customer');
		$phone_number = $this->input->post('phone-number');
		$address = $this->input->post('address');

		$data = array(
				'customer_name'		=>	$customer_name,
				'customer_phone'	=>	$phone_number,
				'shipping_address'	=>	$address,
				'product_id'		=>	$product_id,
			);

		$this->Order_model->insert($data);		

		// get last insert id
		$order_id = $this->db->insert_id();

		//////////////// create order sizes ////////////////
		foreach ($items as $item) 
		{			
			$product_id = $item['id'];			
			$size = $item['options']['Size'];
			$quantity = $item['qty'];

			if(is_numeric($quantity) && $quantity > 0)
			{
				$data = array(
						'order_id'		=>	$order_id,
						'product_id'	=>	$product_id,
						'size'			=>	$size,
						'quantity'		=>	$quantity,
					);

				// create order size
				$this->Order_sizes_model->insert($data);
			}
		}
		////////////////////////////////////////////////////

		///////////////// remove items  ////////////////////
		// list #qty item with size and then set inactive
		$sizes = array('xs' => $xs,'s' => $s,'m' => $m,'l' => $l,'xl' => $xl,'xxl' => $xxl,'xxxl' => $xxxl);

		foreach ($sizes as $key => $value) 
		{
			if($value > 0)
			{
				$product_codei = $this->returnCode($product_code, $key);

				$cond1['product_code'] = $product_codei;

				$itemwithSize = $this->Storage_product_model->loadAllV5($value,0,$cond1);

				$items = $itemwithSize['data'];

				foreach ($items as $i) 
				{
					$id = $i->id;
					$storage_id = $i->storage_id;
					// $data = array('active' => 0);	

					//$this->Storage_product_model->updateEntry($data,$id);

					$this->process($id, $storage_id, $order_id, false);
				}
			}
		}

		$this->session->set_userdata('success','Đặt hàng thành công ! Mã đơn hàng #'.$order_id);		

		redirect('cart/index');
	}

	// export
	public function process($id,$storage_id,$order_id,$redirect = true)
	{				
		// get code from storage_product
		$storage_product = $this->Storage_product_model->getEntry('id', $id);		
		$product_id = $storage_product->product_id;
		$product = $this->Product_model->getEntry($product_id);
		$code = $storage_product->product_code;
		$storage_info = $this->Storage_model->getEntry($storage_product->storage_id);
		$storage_title = $storage_info->title;		
		$product_size = $this->Product_sizes_model->getEntry('code',$code);

		// create record in code_export_form
		$data = array(
				'code'					=>	$code,
				'storage'				=>	$storage_title,
				'order_id'				=>	$order_id,
				'storage_product_id'	=>	$storage_product->id,
				'active'				=>	1,
			);
		$codeport_insertId = $this->Code_export_form_model->insert($data);

		// remove code from storage_product
		$this->Storage_product_model->remove($id);	

		// remove quantity from product_sizes
		$this->Product_sizes_model->updateQuantity($code,'decrease');	

		// increase quantity of store
		$this->Storage_model->updateQuantity($storage_id,'increase');

		// log history		
		$data = array(
				'product'		=>	$product->title,
				'product_id'	=>	$product->id,
				'size'			=>	$product_size->size,
				'code'			=>	$code,
				'storage_id'	=>	$storage_info->id,
				'storage'		=>	$storage_title,
				'type'			=>	2,			
			);
		$history_id = $this->Storage_product_history_model->insertHistory($data);

		// update history id to code_export_form
		$data = array('history_id' => $history_id);

		$this->Code_export_form_model->update($data, $codeport_insertId);	

		$previous_url = $this->session->userdata('currentURL');

		if(empty($previous_url))
			$previous_url = 'exportw/index';
		// return to index
		if($redirect)
			redirect($previous_url);
	}	


	function returnCode($product_code, $size)
	{
		switch ($size) {
			case 'xs':
				return $product_code.'1';
				break;
			case 's':
				return $product_code.'2';
				break;
			case 'm':
				return $product_code.'3';
				break;
			case 'l':
				return $product_code.'4';
				break;
			case 'xl':
				return $product_code.'5';
				break;
			case 'xxl':
				return $product_code.'6';
				break;
			case 'xxxl':
				return $product_code.'7';
				break;				
			default:
				# code...
				break;
		}
	}	
}
